package com.dev.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * @ClassName : ESClientConfig  //类名
 * @Description :   //描述
 * @Author :   //作者
 * @Date: 2021-08-18 10:39  //时间
 */
@Configuration
public class ESClientConfig {

    @Value("${spring.elasticsearch.rest.uris}")
    private String uris;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        String[] split = uris.split(",");
        HttpHost[] httpHosts = new HttpHost[split.length];
        for (int i = 0; i < split.length; i++) {
            if (Objects.isNull(split[i])){
                continue;
            }
            String[] s1 = split[i].split(":");
            HttpHost httpHost = new HttpHost(s1[0], Integer.parseInt(s1[1]));
            httpHosts[i] = httpHost;
        }
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHosts));
        return restHighLevelClient;
    }
}
